package com.jin10.util;

import cn.hutool.core.util.StrUtil;
import com.jin10.vo.TagTreeVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author raofy
 * @date 2022-02-09 17:32
 * @desc
 */
public class Paragraph {

    public static List<String> returnValue(List<TagTreeVo> sentenceLibraryByTag, StringBuffer stringBuffer) {
        return diGuiShengChengWenZhang(sentenceLibraryByTag, stringBuffer);
    }

    /**
     * 递归生成文章
     *
     * @param sentenceLibraryByTag
     * @param stringBuffer
     * @return
     */
    public static List<String> diGuiShengChengWenZhang(List<TagTreeVo> sentenceLibraryByTag, StringBuffer stringBuffer) {
        if (!sentenceLibraryByTag.isEmpty()) {
            ArrayList<String> titles = new ArrayList<>();
            for (TagTreeVo tagTreeVo : sentenceLibraryByTag) {
                returnTitle(tagTreeVo, stringBuffer);
                if (stringBuffer.length() > 0 && stringBuffer.charAt(stringBuffer.length() - 1) == '，') {
                    stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                    stringBuffer.append("。");
                }
                titles.add(stringBuffer.toString());
                stringBuffer = new StringBuffer();
            }
            return titles;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 返回每一句话
     *
     * @param tagTreeVo
     * @param stringBuffer
     */
    public static void returnTitle(TagTreeVo tagTreeVo, StringBuffer stringBuffer) {
        stringBuffer.append(tagTreeVo.getTitle());
        List<TagTreeVo> subclass = tagTreeVo.getSubclass();
        if (!StrUtil.isBlankIfStr(tagTreeVo.getTitle())) {
            stringBuffer.append("，");
        }
        if (subclass != null && !subclass.isEmpty()) {
            for (TagTreeVo treeVo : subclass) {
                returnTitle(treeVo, stringBuffer);
            }
        }
    }
}
